package com.changpay.zywy.ui.activity.serivce

import android.support.v4.app.FragmentTransaction
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.ui.fragment.service.AllowCommitFragment
import kotlinx.android.synthetic.main.activity_rank.*
import kotlinx.android.synthetic.main.title_layout.*

class AllowCommitActivity : BaseActivity() {


    private val TYPE_INDEX: String = "type_index"
    private val FRAGMENT_ONE = 0x01
    private val FRAGMENT_TWO = 0x02
    private val FRAGMENT_Three = 0x03

    private var mIndex = FRAGMENT_ONE

    private var mOneFragment: AllowCommitFragment? = null
    private var mTwoFragment: AllowCommitFragment? = null
    private var mThreeFragment: AllowCommitFragment? = null



    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
                R.id.iv_back -> finish()
        }
    }

    override fun attachLayoutRes(): Int = R.layout.activity_allow_commit

    override fun initData() {
    }

    override fun initView() {
        tv_title.text = "机具调拨"
        iv_back.setOnClickListener(onClickListener)
        type_rg.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId)
            {
                R.id.rb_one-> showFragment(FRAGMENT_ONE)
                R.id.rb_two-> showFragment(FRAGMENT_TWO)
                R.id.rb_three-> showFragment(FRAGMENT_Three)
            }
        }
        showFragment(mIndex)
    }


    override fun start() {


    }





    /**
     * 隐藏所有的Fragment
     */
    private fun hideFragments(transaction: FragmentTransaction) {
        mOneFragment?.let { transaction.hide(it) }
        mTwoFragment?.let { transaction.hide(it) }
        mThreeFragment?.let { transaction.hide(it) }
    }

    /**
     * 展示Fragment
     * @param index
     */
    private fun showFragment(index: Int) {
        val transaction = supportFragmentManager.beginTransaction()
        hideFragments(transaction)
        mIndex = index
        when (index) {
            FRAGMENT_ONE //
            -> {
                if (mOneFragment == null) {
                    mOneFragment = AllowCommitFragment.getInstance(0)
                    transaction.add(R.id.container, mOneFragment!!, "one")
                } else {
                    transaction.show(mOneFragment!!)
                }
            }
            FRAGMENT_TWO //
            -> {
                if (mTwoFragment == null) {
                    mTwoFragment = AllowCommitFragment.getInstance(1)
                    transaction.add(R.id.container, mTwoFragment!!, "two")
                } else {
                    transaction.show(mTwoFragment!!)
                }
            }

            FRAGMENT_Three //
            -> {
                if (mThreeFragment == null) {
                    mThreeFragment = AllowCommitFragment.getInstance(2)
                    transaction.add(R.id.container, mThreeFragment!!, "Three")
                } else {
                    transaction.show(mThreeFragment!!)
                }
            }
        }
        transaction.commit()
    }


}
